combinatorics implementation math matrices number theory *2100

Please click on ads to support us..

C++ Code:

/* In the name of God */

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <list>
#include <map>
#include <numeric>
#include <limits>
#include <limits.h>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <random>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
#define PB push_back
#define POP pop_back
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define endl '\n'
#define dbg(x) cerr << '[' << #x << ": " << x << "]\n"
#define dbg2(x, y) cerr << '[' << #x << ": " << x << ", " << #y << ": " << y << "]\n"
#define Yes cout << "Yes\n"
#define YES cout << "YES\n"
#define No cout << "No\n"
#define NO cout << "NO\n"

const ll INF = (ll)8e18 + 1386;
const ld EPS = 0.000000000000001;
int MOD = 1e9 + 7;

inline int mod_add(int a, int b){ int res = a + b; return (res >= MOD? res - MOD : res); }
inline int mod_neg(int a, int b){ int res = (abs(a - b) < MOD? a - b : (a - b) % MOD); return (res < 0? res + MOD : res); }
inline int mod_mlt(int a, int b){ return (1ll * a * b % MOD); }
inline string intToString(ll a){ char x[100]; sprintf(x, "%lld", a); string s = x; return s; }
inline ll stringToInt(string s){ ll res; char x[100]; strcpy(x, s.c_str()); sscanf(x, "%lld", &res); return res; }
inline void fileIO(string i, string o){ freopen(i.c_str(), "r", stdin); freopen(o.c_str(), "w", stdout); }

const int MAXN = 2e5 + 5;

struct Mat {
    vector<vector<ll>> mat;
    int height, width;
    Mat (int a, int b, ll fill, ll diam = -23){
        height = a, width = b;
        vector<ll> row;
        for (int i = 0; i <= b; i++) row.push_back(fill);
        for (int i = 0; i <= a; i++) mat.push_back(row);
        if (diam != -23) for (int i = 1; i <= a; i++) mat[i][i] = diam;
    };
    Mat (){
        height = width = MAXN - 1; // CHECK THIS
        vector<ll> row;
        for (int i = 0; i <= width; i++) row.push_back(0);
        for (int i = 0; i <= height; i++) mat.push_back(row);
    };
};

Mat mat_mlt(Mat m1, Mat m2){ // i,k,j for square
    int _N = m1.height, _M = m1.width, _K = m2.width;
    Mat res(_N, _K, 0);
    for (int i = 1; i <= _N; i++){
        for (int j = 1; j <= _M; j++){
            for (int k = 1; k <= _K; k++){
                res.mat[i][k] = mod_add(res.mat[i][k], mod_mlt(m1.mat[i][j], m2.mat[j][k]));
            }
        }
    }
    return res;
}

Mat mat_pow(Mat m1, ll n){
    if (!n) return Mat(m1.height, m1.width, 0, 1);
    Mat res = mat_pow(m1, n >> 1);
    res = mat_mlt(res, res);
    if (n & 1) res = mat_mlt(res, m1);
    return res;
}

ll poww(ll x, ll y, int p = MOD){ // OOPS!
    ll res = 1;
    x %= p;
    while (y > 0){
        if (y & 1)
            res = (res * x) % p;
        y >>= 1;
        x = (x * x) % p;
    }
    return res;
}

ll _mlt(ll x, ll y){
    if (x == 0 || y == 0) return 0;
    if (x > INF / y) return INF;
    return x * y;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll n, k; int l;
    cin >> n >> k >> l >> MOD;
    ll _p2 = 1;
    for (int i = 1; i <= l; i++){
        _p2 = _mlt(_p2, 2);
    }
    if (k >= _p2) return cout << 0, 0;

    Mat fib(2, 2, 1); fib.mat[2][2] = 0;
    fib = mat_pow(fib, n + 1);

    int ans0 = fib.mat[1][1];
    int ans1 = mod_neg(poww(2, n), ans0);
    int c[2] = {0, 0};
    for (int i = 0; i < l; i++){
        int B = (k >> 1ll * i) & 1;
        c[B]++;
    }
    cout << mod_mlt(poww(ans0, c[0]), poww(ans1, c[1]));
    return 0;
}


Comments

Submit
0 Comments
More Questions

Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time